package com.rq.base.util;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Environment;

import com.rq.base.BASE;
import com.rq.base.R;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * Created by RaoQian on 2022/8/29.
 */
public class WriteUtil {


    SimpleDateFormat d2Hour = new SimpleDateFormat("yyyy_MM_dd_HH");

    /**
     * 保证只有一个CrashHandler实例
     */
    private WriteUtil() {
    }

    private static WriteUtil instance;

    /**
     * 获取WriteUtil实例 ,单例模式
     */
    public static WriteUtil getInstance() {
        if (instance == null) {
            synchronized (WriteUtil.class) {
                if (instance == null)
                    instance = new WriteUtil();
            }
        }
        return instance;
    }

    private String getSavePath() {
        return Environment.getExternalStorageDirectory() + "/rq";
    }

    public boolean hasWritePermission() {
        PackageManager pm = BASE.getCxt().getPackageManager();
        return (PackageManager.PERMISSION_GRANTED ==
                pm.checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, BASE.getCxt().getPackageName()));
//        if (permission) {
//            showToast("有这个权限");
//        } else {
//            showToast("木有这个权限");
//        }
    }

    public void writeLog(String sb) {
        if (!hasWritePermission()) return;
        try {
            String time = d2Hour.format(System.currentTimeMillis());
            String fileName = BASE.getCxt().getString(R.string.app_name) + "_" + time + ".txt";
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                String path = getSavePath() + "/";
                File dir = new File(path);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                File txtFile = new File(path + fileName);
                if (!txtFile.exists()) {
                    new File(path + fileName).createNewFile();
                    FileOutputStream fos = new FileOutputStream(path + fileName);
                    fos.write(sb.getBytes());
                    fos.close();
                } else {
                    addTxtToFileWrite(txtFile, sb);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void writeLog(String name, String sb) {
        if (!hasWritePermission()) return;
        try {
            String time = d2Hour.format(System.currentTimeMillis());
            String fileName = name + "_" + time + ".txt";
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                String path = getSavePath() + "/";
                File dir = new File(path);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                File txtFile = new File(path + fileName);
                if (!txtFile.exists()) {
                    new File(path + fileName).createNewFile();
                    FileOutputStream fos = new FileOutputStream(path + fileName);
                    fos.write(sb.getBytes());
                    fos.close();
                } else {
                    addTxtToFileWrite(txtFile, sb);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private boolean addTxtToFileWrite(File file, String content) {
        FileWriter writer = null;
        try {
            //FileWriter(file, true),第二个参数为true是追加内容，false是覆盖
            writer = new FileWriter(file, true);
            writer.write("\r\n");//换行
            writer.write(content);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

}
